
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function loadData() {
    var _maKM = ISS.SESSION.getSession('session_maKM');
    var _maCTKM = ISS.SESSION.getSession('session_maCTKM');

    var deal = {};
    deal.maKM = _maKM;

    var dealdetail = {};
    dealdetail.maCTKM = _maCTKM;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editDeal',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(deal),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editDealDetail',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(dealdetail),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataDetailComplete(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllMemberShowInDeal',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllMemberToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllDealDetailShowInDetail',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapDealDetailToView(responseContent);
        }
    });

/*    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllProductDetailShowInDetail',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapProductDetailToView(responseContent);
        }
    });*/

}

function loadDataComplete(deal){
    ISS.DOM.setVal('#DealCommon_code', deal.maKM);
    ISS.DOM.setVal('#DealCommon_user_hidden', deal.thongTinThanhVien.maTV);
    ISS.DOM.setVal('#DealCommon_name', deal.tenKM);
    ISS.DOM.setVal('#DealCommon_dateCreate', ISS.DATE.getFullDate(new Date(deal.ngayTaoKM)));
    ISS.DOM.setVal('#DealCommon_dateBegin', ISS.DATE.getFullDate(new Date(deal.ngayBD)));
    ISS.DOM.setVal('#DealCommon_dateEnd', ISS.DATE.getFullDate(new Date(deal.ngayKT)));
}

function loadDataDetailComplete(deal){
    ISS.DOM.setVal('#DealDetail_code_hidden', deal.maCTKM);
    ISS.DOM.setVal('#DealDetail_name_hidden', deal.khuyenMai.maKM);
    ISS.DOM.setVal('#DealDetail_product_hidden', deal.chiTietSanPham.maCTSP);
    ISS.DOM.setVal('#DealDetail_PriceBefore', deal.chiTietSanPham.giaSP);
    ISS.DOM.setVal('#DealDetail_Percent', deal.phanTram);
}


function bindEventEdit(){
    ISS.EVENT.bindEvent('#DealCommon_Modified1', 'click', function () {
        ISS.DOM.removeClass('#boxTrackUpdateConfirm', 'hideBox');
    });

    ISS.EVENT.bindEvent('#DealDetail_Modified1', 'click', function () {
        sendDetailInfoEdit();
    });

    ISS.EVENT.bindEvent('#buttonUpdateDealCommon_Agree', 'click', function () {
        ISS.DOM.addClass('#boxTrackUpdateConfirm', 'hideBox');
        sendInfoEdit();
    });

    ISS.EVENT.bindEvent('#buttonUpdateDealCommon_Cancel', 'click', function () {
        ISS.DOM.addClass('#boxTrackUpdateConfirm', 'hideBox');
    });

    ISS.EVENT.bindEvent('#buttonUpdateDealCommonSuccess_Agree', 'click', function () {
        ISS.DOM.addClass('#boxTrackUpdateSuccessConfirm', 'hideBox');
    });

}

function sendInfoEdit() {
    var deal = {};
    var member = {};

    deal.maKM = ISS.DOM.getVal('#DealCommon_code');
    member.maTV  = ISS.DOM.getVal('#DealCommon_user_option');
    deal.thongTinThanhVien = member;
    deal.tenKM = ISS.DOM.getVal('#DealCommon_name');

    deal.ngayTaoKM = ISS.DATE.changeFormatDate(ISS.DOM.getVal('#DealCommon_dateCreate'));
    deal.ngayBD = ISS.DATE.changeFormatDate(ISS.DOM.getVal('#DealCommon_dateBegin'));
    deal.ngayKT = ISS.DATE.changeFormatDate(ISS.DOM.getVal('#DealCommon_dateEnd'));

/*    deal.ngayTaoKM = ISS.DOM.getVal('#DealCommon_dateCreate');
    deal.ngayBD = ISS.DOM.getVal('#DealCommon_dateBegin');
    deal.ngayKT = ISS.DOM.getVal('#DealCommon_dateEnd');*/

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerDealEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(deal),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoEditComplete(responseContent);
        }
    });
}

function sendDetailInfoEdit() {
    var deal = {};
    var product = {};
    var name = {};

    deal.maCTKM = ISS.DOM.getVal('#DealDetail_code_hidden');

    product.maCTSP  = ISS.DOM.getVal('#DealDetail_product_option');
    deal.chiTietSanPham = product;

    name.maKM = ISS.DOM.getVal('#DealDetail_name_option');
    deal.khuyenMai = name;

    deal.phanTram = ISS.DOM.getVal('#DealDetail_Percent');

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerDealDetailEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(deal),
        success: function (data) {
            var responseContent = data.responseObject;
            sendDetailInfoEditComplete(responseContent);
        }
    });
}

function sendDetailInfoEditComplete(color) {
    if(color) {
        ISS.WINDOW.location('/jsp/admin/ad-deal-detail-common.jsp');
    }
    else{
        alert('Cập nhật không thành công');
    }
}

function sendInfoEditComplete(message) {
/*    if(color) {
        ISS.WINDOW.location('/jsp/admin/ad-deal-common.jsp');
    }
    else{
        alert('Cập nhật không thành công');
    }*/
    if(message){
        ISS.DOM.removeClass('#boxTrackUpdateSuccessConfirm', 'hideBox');
    }
    else{
        alert('Cập nhật thất bại');
    }
}

function mapAllMemberToView(member) {
    var code = ISS.DOM.getVal('#DealCommon_user_hidden');
    var n = '';
    var html = '<option value="default_value">...</option>';
    for(var i=0; i<member.length; i++) {
        if (member[i].maTV == code){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + member[i].maTV + '" ' + n + '  >' + member[i].maTV + ' - ' + member[i].ho + ' ' + member[i].ten + '</option>';
    }
    ISS.DOM.html('#DealCommon_user_option', html);
}

function mapDealDetailToView(listdeal) {

    var deal = ISS.DOM.getVal('#DealDetail_name_hidden');
    var n = '';
    var html = '';
    for(var i=0; i<listdeal.length; i++) {
        if (listdeal[i].maKM == deal){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + listdeal[i].maKM + '"' + n + '>' + listdeal[i].tenKM + '</option>';
    }
    ISS.DOM.html('#DealDetail_name_option', html);
}

function mapProductDetailToView(listproduct) {

    var product = ISS.DOM.getVal('#DealDetail_product_hidden');
    var n = '';
    var html = '';
    for(var i=0; i<listproduct.length; i++) {
        if (listproduct[i].maCTSP == product){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + listproduct[i].maCTSP + '"' + n + ' >' + listproduct[i].sanPham.tenSP + ' - ' + listproduct[i].mauSac.tenMau + ' - ' + listproduct[i].kichThuoc.tenSize + ' - ' + listproduct[i].sanPham.doiTuong + '</option>';
    }
    ISS.DOM.html('#DealDetail_product_option', html);
}